<template>
  <div class="box">
    <div class="form-content">
      <el-form :model="ruleForm" label-width="100px" size="small" :rules="rules" ref="ruleForm">
        <el-form-item label="帮扶类型">
          <div class="button-list f-r a-c f-w">
            <div
              v-for="item in buttonList"
              :key="item.id"
              :class="['button-item', item.id === type? 'active' : '']"
              @click="type = item.id"
            >
              {{ item.name }}
            </div>
          </div>
        </el-form-item>
        <el-form-item v-show="type === 3" label="联报类型">
          <el-select v-model="ruleForm.type">
            <el-option v-for="item in typeList" :key="item.id" :label="item.name" :value="item.id" />
          </el-select>
        </el-form-item>
        <el-form-item v-show="type === 2" label="帮办类型">
          <el-select v-model="ruleForm.atype">
            <el-option v-for="item in atypeList" :key="item.id" :label="item.name" :value="item.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="联系人" prop="contacts">
          <el-input v-model="ruleForm.contacts" />
        </el-form-item>
        <el-form-item label="联系方式" prop="mobile">
          <el-input v-model="ruleForm.mobile" />
        </el-form-item>
        <el-form-item v-show="type === 3" label="联报内容">
          <el-input v-model="ruleForm.guaranty" type="textarea" />
        </el-form-item>
        <el-form-item label="需求描述" prop="content">
          <el-input v-model="ruleForm.content" type="textarea" />
        </el-form-item>

        <el-form-item label="资料上传">
          <van-uploader v-model="fileList" :max-count="1" :after-read="afterRead" />
        </el-form-item>
      </el-form>
    </div>
    <el-button type="primary" style="width: 100%;margin-top: 15px" @click="onSave">确认提交</el-button>
  </div>
</template>

<script>
import { http_project_node, uploadFile } from '@/api'
import { Toast } from 'vant'
export default {
  name: 'Consulting',
  data() {
    return {
      type: 1,
      ruleForm: {
        type: 1,
        atype: 1,
        contacts: '',
        mobile: '',
        guaranty: '',
        content: ''
      },
      typeList: [
        { id: 1, name: '通水' },
        { id: 2, name: '通电' },
        { id: 3, name: '通气' },
        { id: 4, name: '通网络' }
      ],
      buttonList: [
        { id: 1, name: '计划变更' },
        { id: 2, name: '精准帮办' },
        { id: 3, name: '外线联报' }
      ],
      atypeList: [
        { id: 1, name: '模拟审批' },
        { id: 2, name: '外线报装' },
        { id: 3, name: '预验收服务' },
        { id: 4, name: '联合验收服务' },
        { id: 5, name: '旧厂区改造服务' },
        { id: 6, name: '全程帮办' }
      ],

      fileList: [],
      fileUrl: '',
      rules: {
        mobile: [ //
          { required: true, message: '请输入联系方式', trigger: 'blur' }
        ],
        contacts: [ //
          { required: true, message: '请输入联系人', trigger: 'blur' }
        ],
        content: [ //
          { required: true, message: '请输入需求描述', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    afterRead(file) {
      file.status = 'uploading'
      file.message = '上传中...'
      uploadFile(file.file).then(res => {
        file.url = res.imgUrl
        file.status = 'success'
        this.fileUrl = res.imgUrl
        this.fileList = [
          {
            fileName: res.fileName,
            fileType: res.suffix,
            fileUrl: res.imgUrl }
        ]
      }).catch((err) => {
        file.status = 'failed'
        file.message = '上传失败'
        Toast(err)
      })
    },
    onSave() {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          if (this.type === 1) {
            http_project_node.createPlan({
              contacts: this.ruleForm.contacts,
              mobile: this.ruleForm.mobile,
              content: this.ruleForm.content,
              projectId: this.$route.query.projectId,
              file: JSON.stringify(this.fileUrl)
            }).then(res => {
              Toast(res.msg)
              this.ruleForm = {
                type: 1,
                atype: 1,
                contacts: '',
                mobile: '',
                guaranty: '',
                content: ''
              }
              this.fileList = []
              this.fileUrl = ''
            })
          } else if (this.type === 2) {
            http_project_node.createHelp({
              atype: this.ruleForm.atype,
              contacts: this.ruleForm.contacts,
              mobile: this.ruleForm.mobile,
              content: this.ruleForm.content,
              projectId: this.$route.query.projectId,
              file: JSON.stringify(this.fileUrl)
            }).then(res => {
              Toast(res.msg)
              this.ruleForm = {
                type: 1,
                atype: 1,
                contacts: '',
                mobile: '',
                guaranty: '',
                content: ''
              }
              this.fileList = []
              this.fileUrl = ''
            })
          } else if (this.type === 3) {
            http_project_node.createLine({
              type: this.ruleForm.type,
              guaranty: this.ruleForm.guaranty,
              contacts: this.ruleForm.contacts,
              mobile: this.ruleForm.mobile,
              content: this.ruleForm.content,
              projectId: this.$route.query.projectId,
              file: JSON.stringify(this.fileUrl)
            }).then(res => {
              Toast(res.msg)
              this.ruleForm = {
                type: 1,
                atype: 1,
                contacts: '',
                mobile: '',
                guaranty: '',
                content: ''
              }
              this.fileList = []
              this.fileUrl = ''
            })
          }
        }
      })
    }
  }
}
</script>

<style scoped lang="scss">
.box {
    padding: 20px;
    .el-select,
    .el-input {
        width: 100%;
    }
    .form-content {
        width: 100%;
        background: #FFFFFF;
        border-radius: 10px;
        padding: 27px 10px 27px 0;
    }
    .button-list {
        .button-item {
            margin-right: 13px;
            width: 60px;
            height: 26px;
            border: 1px solid #2F86FD;
            border-radius: 12px;
            line-height: 24px;
            font-size: 12px;
            font-family: PingFang SC;
            font-weight: 500;
            color: #2F86FD;
            text-align: center;
        }
        .button-item.active {
            background: #2F86FD;
            color: #fff
        }
    }
}
</style>
